<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/25
 * Time: 10:33
 */
namespace app\admin\controller;

use app\admin\model\Subject;
use think\Controller;
use app\admin\model\Users as User;
use think\Db;
use think\Request;

class Users extends Controller{
    public function show_list(Request $request){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('users/del','全部删除')
            ->addTableHeaderSousuo('')
            ->addTableColumn('id','text','ID',null,'5%')
            ->addTableColumn('user','text','用户名',null,'10%')
            ->addTableColumn('touxiang','img','头像')
            ->addTableColumn('status','switch','状态',['启用','禁用'],'users/change')
            ->addTableRightHeader('操作')
            ->addTableRight('detail','users/detail','查看')
            ->addTableRight('edit','users/show_edit','编辑')
            ->addTableRight('del','users/del','删除')
            ->addTablePage('users/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit');
        $data = User::where('id','>',0);
        if($tiaojian){
            $data = $data->where('user|dir','like',"%%$tiaojian");
        }
        $data = $data->order('sort desc,create_time desc')
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
           $value['touxiang'] = getFilePath($value['touxiang']);
           $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**修改数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function change(Request $request){
        $id = $request->param('id');
        $user = User::where('id',$id)->findOrEmpty()->toArray();
        if($user['status']==1){
            $user['status'] = 0;
        }else{
            $user['status'] = 1;
        }
        if(User::where('id',$user['id'])->update([
            'status'=>$user['status']
        ])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**编辑用户
     * @param Request $request
     * @return mixed
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $user = User::where('id',$id)->findOrEmpty()->toArray();
        if($user){
            if($user['edit']==0){
                echo '本数据不可编辑';exit;
            }
        }
        $obj = new Subject();
        $obj->setModelId(14);
        return $obj->getLianBiaoForm(14,null,$id,'edit',true,'users/edit');
    }

    /**编辑用户
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        unset($data['model_id']);
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(isset($data['edit'])&&$data['edit']=='on'){
            $data['edit'] = 1;
        }else{
            $data['edit'] = 0;
        }
        if($data['pwd']){
            $data['pwd'] = my_encryption($data['pwd'],true);
        }else{
            unset($data['pwd']);
        }
        $data['create_time'] = strtotime($data['create_time']);
        $data['update_time'] = time();
        if(User::where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    /**
     * @param Request $request
     * @return mixed
     */
    public function detail(Request $request){
        $id = $request->param('id');
//        $user = User::where('id',$id)->findOrEmpty()->toArray();
        $obj = new Subject();
        $obj->setModelId(14);
        return $obj->getLianBiaoForm(14,null,$id,null,false);
    }
    public function del(Request $request){
        $id = $request->param('id');
        $user = User::where('id','in',$id)->select()->toArray();
        foreach ($user as $value){
            if($value['edit']==0){
                return json_my_response('有不可删除数据');
            }
        }
//        User::where('id','in',$id)->delete();
        if(Db::name(session('modular').'_users')->where('id','in',$id)->update([
            'delete_time'=>1
        ])){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}